package com.sl.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public abstract class DB implements Find{
	final private static String url = "jdbc:mysql://localhost:3306/sl_demol";
	final private static String user = "root";
	final private static String pwd = "123456";
	final private static String driver = "com.mysql.jdbc.Driver";
	protected Connection conn = null;
	protected PreparedStatement pst =null;
	protected ResultSet rs = null;
	static {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	public void open() throws SQLException {
		conn=DriverManager.getConnection(url, user, pwd);
	}
	public void chose() {
		try {
			if (rs!=null) {
				rs.close();
			}
			if (pst!=null) {
				pst.close();
			}
			if (conn!=null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public boolean Update(String sql) {
		int rss=0;
		try {
			open();
			pst=conn.prepareStatement(sql);
			rss=pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			chose();
		}
		return rss>0;
	}
	@Override
	public abstract List<Object> find(String sql);
}
